package com.example.demo.Controller;

import com.example.demo.Service.TUserService;
import com.example.demo.entity.TUser;
import com.example.demo.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class TUserController {
    private TUserService tU;
@Autowired
    public void settU(TUserService tU) {
        this.tU = tU;
    }

    @RequestMapping("denglu")
    public Result denglu (@RequestBody TUser user){
        System.out.println(user);
        TUser tUser=tU.denglu(user);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUsername(),
                user.getPasswd()
        );
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
            subject.hasRole("登录");
        } catch (Exception e) {
            System.out.println(456456);
            return Result.error();
        }
        return !(tUser==null)?Result.ok().data("user",tUser):Result.error();
    }
    @PostMapping("logout")
    public Result dengchu(){
    return Result.ok();
    }
}
